package com.qida.clm.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{
	
	// 多线程中慎用
	public final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	public final static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd HH:mm");
	public final static SimpleDateFormat sdf3 = new SimpleDateFormat("HH:mm");
	public final static SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public final static SimpleDateFormat sdf5 = new SimpleDateFormat("MM-dd HH:mm");
	public final static SimpleDateFormat sdf6 = new SimpleDateFormat("yyyy-MM-dd");
	public final static int TIME_PAST = -1;
	public final static int TIME_UNDER_WAY = 0;
	public final static int TIME_FUTURE = 1;
	
	public static String getDateStrSync()
	{
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return simpleDateFormat.format(new Date());
	}
	
	/** 格式化时间*/
	public static String parseDate(String createTime)
	{
		String ret = "";
		try
		{
			Date create = sdf.parse(createTime);
			long createMillis = create.getTime();
			Calendar c0 = Calendar.getInstance();
			c0.setTime(sdf.parse(createTime));
			
			Calendar c1 = Calendar.getInstance();
			c1.set(Calendar.DAY_OF_YEAR, c1.get(Calendar.DAY_OF_YEAR)-1);
			Date yesterday = c1.getTime();
			
			Calendar c2 = Calendar.getInstance();
			c2.setTime(new Date());
			Date today = c2.getTime();
			
			if(sdf6.format(today).equals(sdf6.format(c0.getTime()))){
				ret = "今天 " + sdf3.format(createMillis);
			}else if(sdf6.format(yesterday).equals(sdf6.format(c0.getTime()))){
				ret = "昨天 " + sdf3.format(createMillis);
			}else if(c0.get(Calendar.YEAR) ==c2.get(Calendar.YEAR)) {
				System.out.println(createTime);
				ret = sdf5.format(createMillis);
			}else {
				ret = sdf4.format(createMillis);
			}
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		return ret;
}
	
	/**
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static boolean isSameDay(Date startDate, Date endDate)
	{
		boolean result = false;
		SimpleDateFormat sdf4 = new SimpleDateFormat("yyyyMMdd");
		if (sdf4.format(startDate).equals(sdf4.format(endDate)))
		{
			result = true;
		}
		return result;
	}
	
	/**
	 * 
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static String getBetweenDateStr(Date startDate, Date endDate)
	{
		StringBuffer sb = new StringBuffer();
		sb.append(sdf2.format(startDate)).append("-");
		if (isSameDay(startDate, endDate))
		{
			sb.append(sdf3.format(endDate));
		}
		else
		{
			sb.append(sdf2.format(endDate));
		}
		return sb.toString();
	}
	
//	/**
//	 * 
//	 * @param startTime
//	 * @param endTime
//	 * @return
//	 * 
//	 * 
//	 */
//	public static int underWayStatus(Date startTime, Date endTime)
//	{
//		
//		long startTimeStamp = startTime.getTime();
//		long endTimeStamp = endTime.getTime();
//		long nowTimeStamp;
//		
//		if (HttpInfoClient.mServerDate != null)
//		{
//			nowTimeStamp = HttpInfoClient.mServerDate.getTime();
//		}
//		else
//		{
//			nowTimeStamp = System.currentTimeMillis();
//		}
//		
//		if (nowTimeStamp >= endTimeStamp)
//		{
//			return TIME_PAST;
//		}
//		
//		if (startTimeStamp > nowTimeStamp)
//		{
//			return TIME_FUTURE;
//		}
//		return TIME_UNDER_WAY;
//	}
	// 秒=1000毫秒
	private static final int SECOND = 1000;
	// 分钟=60秒
	private static final int MIMUTE = SECOND * 60;
	// 小时=60分钟
	private static final int HOUR = MIMUTE * 60;
	// 天=24小时
	private static final int DAY = HOUR * 24;
	
	/**
	 * 将毫秒数换算成x天x时x分x秒x毫秒
	 * 
	 * @param ms
	 * @return
	 */
	public static String formatMs2String(long ms)
	{
		
		long day = ms / DAY;
		long hour = (ms - day * DAY) / HOUR;
		long minute = (ms - day * DAY - hour * HOUR) / MIMUTE;
		long second = (ms - day * DAY - hour * HOUR - minute * MIMUTE) / SECOND;
		long milliSecond = ms
				- day * DAY - hour * HOUR - minute * MIMUTE - second * SECOND;
		
		StringBuffer sb = new StringBuffer();
		
		if (day > 0)
		{
			sb.append(day).append("天");
			if (hour > 0)
			{
				sb.append(hour).append("小时");
			}
			return sb.toString();
		}
		
		if (hour > 0)
		{
			sb.append(hour).append("小时");
			if (minute > 0)
			{
				sb.append(minute).append("分");
			}
			
			return sb.toString();
		}
		
		if (minute > 0)
		{
			sb.append(minute).append("分");
			return sb.toString();
		}
		
		if (second > 0)
		{
			sb.append(second).append("秒");
			return sb.toString();
		}
		
		sb.append(milliSecond).append("毫秒");
		return sb.toString();
		
	}
	/**
	 * 将秒数换算成x天x时x分x秒
	 * 
	 * @param ms
	 * @return
	 */
	public static String formatSec2String(long ms)
	{
		ms = ms*1000;
		long day = ms / DAY;
		long hour = (ms - day * DAY) / HOUR;
		long minute = (ms - day * DAY - hour * HOUR) / MIMUTE;
		long second = (ms - day * DAY - hour * HOUR - minute * MIMUTE) / SECOND;
		
		StringBuffer sb = new StringBuffer();
		
		if (day > 0)
		{
			sb.append(day).append("天");
			if (hour > 0)
			{
				sb.append(hour).append("小时");
			}
			return sb.toString();
		}
		
		if (hour > 0)
		{
			sb.append(hour).append("小时");
			if (minute > 0)
			{
				sb.append(minute).append("分");
			}
			
			return sb.toString();
		}
		
		if (minute > 0)
		{
			sb.append(minute).append("分");
			return sb.toString();
		}
		
		if (second > 0)
		{
			sb.append(second).append("秒");
			return sb.toString();
		}
		return sb.toString();
		
	}
//	/** 今天是否提交了登录日志*/
//	public static boolean createLog(){
//		boolean result = true;
//		PreferencesManager preferencesManager = PreferencesManager.getInstance();
//		String login_date = preferencesManager.getValue(Constant.LOGIN_DATE);
//		if(TextUtils.isEmpty(login_date)||(!sdf6.format(new Date()).equals(login_date))){
//			preferencesManager.cleanAndAdd(Constant.LOGIN_DATE, sdf6.format(new Date()));
//		}else {
//			result = false;
//		}
//		return result;
//	}
}
